using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.FileManipulation.Properties;

namespace Workflows.Components.FileManipulation
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class CryptionComponent:WorkflowComponentBase
    {

        #region props

        private CryptionInstruction _Instruction;
        private CryptionOutput _Output;

        /// <summary>
        /// 
        /// </summary>
        public string SrcPath
        {
            get { return this._Instruction.SrcPath; }
            set { this._Instruction.SrcPath = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string TgtPath
        {
            get { return this._Instruction.TgtPath; }
            set { this._Instruction.TgtPath = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Password
        {
            get { return this._Instruction.Password; }
            set { this._Instruction.Password = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string PublicKey
        {
            get { return this._Instruction.PublicKey; }
            set { this._Instruction.PublicKey = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string PrivateKey
        {
            get { return this._Instruction.PrivateKey; }
            set { this._Instruction.PrivateKey = value; }
        }

        public override InstructionBase Instruction
        {
            get { return this._Instruction; }
            set { this._Instruction = (CryptionInstruction) value; }
        }

        public override ResultBase Output
        {
            get { return this._Output; }
            set { this._Output = (CryptionOutput) value; }
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        public CryptionComponent():base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.File_Tasks.ToString();
            this.Description = "Encrypt/decrypt a file or folder";
            this.ComponentIcon = Resources.password;
            this.ComponentImage = Resources.Lock;
            this.ComponentName = "Encrypt/Decrypt task";

            this._Instruction =
                new CryptionInstruction(this.ComponentID, Guid.Empty, string.Empty, string.Empty, string.Empty,
                                        string.Empty, string.Empty);
            this._Output=new CryptionOutput(this.ComponentID,Guid.Empty,string.Empty);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SrcPath", this.SrcPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtPath", this.TgtPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Password", this.Password);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "PubKey", this.PublicKey);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "PrivateKey", this.PrivateKey);

            return xNode;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xNode"></param>
        /// <returns></returns>
        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                CryptionComponent cryptComponent = base.Instantiate(xNode) as CryptionComponent;

                cryptComponent.SrcPath = xNode.Attributes.GetNamedItem("SrcPath").Value;
                cryptComponent.TgtPath = xNode.Attributes.GetNamedItem("TgtPath").Value;
                cryptComponent.Password = xNode.Attributes.GetNamedItem("Password").Value;
                cryptComponent.PublicKey = xNode.Attributes.GetNamedItem("PubKey").Value;
                cryptComponent.PrivateKey = xNode.Attributes.GetNamedItem("PrivateKey").Value;
                return cryptComponent;
            }
            else
            {
                throw new Exception("Invalid type to instantiate");
            }
        }
    }
}
